package day09;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author げっしょく
 * @date 2021/7/10
 */
public class ArrayStuden01 {
    public static void idset(ArrayList<Studen01> list , Studen01 stu1) {
        Studen01 stu2 = new Studen01();
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < list.size(); i++) {
            stu2 = list.get(i);
            if (stu1.getId().equals(stu2.getId())) {
                System.out.println("您输入的id有误，请重新输入");
                stu1.setId(sc.next());
                idset(list, stu1);
            }
        }
    }
    public static String idset(ArrayList<Studen01> list , String id) {
        Studen01 stu2 = new Studen01();
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < list.size(); i++) {
            stu2 = list.get(i);
            if (id.equals(stu2.getId())) {
                System.out.println("您输入的id有误，请重新输入");
                id = sc.next();
                id = idset(list, id);
            }
        }
        return id;
    }
    public static void addStudent(ArrayList<Studen01> list){
        Scanner sc = new Scanner(System.in);
        Studen01 stu1 = new Studen01();
        System.out.println("请输入要添加学生的姓名：");
        stu1.setName(sc.next());
        System.out.println("请输入要添加学生的学号：");
        stu1.setId(sc.next());
        idset(list, stu1);
        System.out.println("请输入要添加学生的年龄：");
        stu1.setAge(sc.nextInt());

//        if(idset){
//            list.add(stu1);
//            System.out.println("添加成功");
//        }else{
//            System.out.println("添加失败");
//        }

        list.add(stu1);
    }
    public static void deletestu(ArrayList<Studen01> list){
        Scanner sc = new Scanner(System.in);
        Studen01 stu1 = new Studen01();
//        System.out.println("请输入要删除学生的姓名：");
//        String name = sc.next();
        System.out.println("请输入要删除学生的学号：");
        String id = sc.next();
//        System.out.println("请输入要删除学生的年龄：");
//        int age = sc.nextInt();
        for (int i = 0; i < list.size(); i++) {
            Studen01 stu = (Studen01) list.get(i);
            if(id.equals(stu.getId())){
                list.remove(i);
                return ;
            }
//            if(name.equals(stu.getName()) && id.equals(stu.getId()) && age == stu.getAge()){
//                list.remove(i);
//            }
        }
        System.out.println("你输入的学号不存在");
    }
    public static Studen01 setStudent(ArrayList<Studen01> list){
        Scanner sc = new Scanner(System.in);
        Studen01 stu1 = new Studen01();
//        System.out.println("请输入要修改信息学生的姓名：");
//        stu1.setName(sc.next());
        System.out.println("请输入修改信学生的学号：");
        stu1.setId(sc.next());
        for (int i = 0; i < list.size(); i++) {
            Studen01 stu2 = list.get(i);
            if(stu1.getId().equals(stu2.getId())){
                System.out.println("请输入修改后学生的姓名：");
                stu2.setName(sc.next());
                System.out.println("请输入修改后学生的学号：");
                String id =sc.next();
                id = idset(list, id);
                stu2.setId(id);
                System.out.println("请输入修改后学生的年龄：");
                stu2.setAge(sc.nextInt());
                list.set(i,stu2);
            }else if(i == list.size()-1){
                System.out.println("你输入的学号不存在");
            }
        }

//        System.out.println("请输入修改信息学生的年龄：");
//        stu1.setAge(sc.nextInt());
//        Studen01 stu2 = new Studen01();
//        System.out.println("请输入修改后学生的姓名：");
//        stu2.setName(sc.next());
//        System.out.println("请输入修改后学生的学号：");
//        stu2.setId(sc.next());
//        System.out.println("请输入修改后学生的年龄：");
//        stu2.setAge(sc.nextInt());
//        for (int i = 0; i < list.size(); i++) {
//            Studen01 stu3 = list.get(i);
//            if(stu1.getName().equals(stu3.getName()) && stu1.getId().equals(stu3.getId()) && stu1.getAge() == stu3.getAge()){
//                list.remove(i);
//                list.add(i,stu2);
//            }
//        }
        return stu1;
    }
    public static void printList(ArrayList<Studen01> list){
        if(list.size() == 0){
            System.out.println("管理系统中还没有数据，请先添入数据");
            return;
        }
        System.out.println("学生姓名\t学号\t年龄");
        for (int i = 0; i < list.size(); i++) {
            Studen01 stu = list.get(i);
            System.out.println(stu.getName()+ "\t" + stu.getId() + "\t" + stu.getAge());
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String zhanghu = "gongshang666";
        String mima = "zheshigefeiwu";
        System.out.println("请输入账号：");
        String s1 = sc.next();
        System.out.println("请输入密码：");
        String s2 = sc.next();
        if(zhanghu.equals(s1) && mima.equals(s2)){
            int a =0;
            ArrayList<Studen01> list = new ArrayList<>();
            io:while (true) {
                System.out.println("-------欢迎使用学生管理系统-------");
                System.out.println("1 增加学生信息");
                System.out.println("2 删除学生信息");
                System.out.println("3 修改学生信息");
                System.out.println("4 查看学生信息");
                System.out.println("5 退出");

                a = sc.nextInt();
                switch(a) {
                    case 1:
                        addStudent(list);
                        break;
                    case 2:
                        deletestu(list);
                        break;
                    case 3:
                        setStudent(list);
                        break;
                    case 4:
                        printList(list);
                        break;
                    case 5:
                        break io;
                    default:
                        System.out.println("你输入的数字错误！请重新输入：");
                }

            }
        }else{
            System.out.println("输入的账户密码错误");
        }

    }
}
/*
//创建一个方法，把年龄小于18 的放到一个新集合里面
public static void main(String[] args) {
        ArrayList<Studen01> list = new ArrayList<>();
        ArrayList<Studen01> studen = new ArrayList<>();
        studen = getlist(list, studen);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).name+"...."+list.get(i).age);
        }
        System.out.println("--------------------");
        for (int i = 0; i < studen.size(); i++) {
            System.out.println(studen.get(i).name+"...."+studen.get(i).age);
        }
    }

    private static ArrayList<Studen01> getlist(ArrayList<Studen01> list, ArrayList<Studen01> studen) {
        Studen01 stu1 = new Studen01("zhangsan","65",25);
        Studen01 stu2 = new Studen01("lisi","65",21);
        Studen01 stu3 = new Studen01("wangwu","65",14);
        Studen01 stu4 = new Studen01("zhaoliu","65",19);
        Studen01 stu5 = new Studen01("qiantan","65",12);
        Studen01 stu6 = new Studen01("yaojiu","65",11);
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        list.add(stu4);
        list.add(stu5);
        list.add(stu6);
        for (int i = 0; i < list.size(); i++) {
            Studen01 stu = list.get(i);
            if(stu.age<18){
                studen.add(stu);
            }
        }
        return studen;
    }
 */









/*
ArrayList<String> list = new ArrayList<>();
        list.add("test");
        list.add("zhe");
        list.add("shi");
        list.add("wang");
        list.add("test");
        list.add("test");
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if("test".equals(s)){
                list.remove(i);
                i--;
            }
        }
        System.out.println(list);
 */





/*
Scanner sc = new Scanner(System.in);
        ArrayList<Studen01> list = new ArrayList<>();
        Studen01 s1 = new Studen01(sc.next(),sc.next(),sc.nextInt());
        Studen01 s2 = new Studen01(sc.next(),sc.next(),sc.nextInt());
        Studen01 s3 = new Studen01(sc.next(),sc.next(),sc.nextInt());
        list.add(s1);
        list.add(s2);
        list.add(s3);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).name);
            System.out.println(list.get(i).id);
            System.out.println(list.get(i).age);
        }
 */